package com.hn.project.rqinspect.service.impl;

import com.hn.framework.redis.RedisCache;
import com.hn.project.rqinspect.service.TaskCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
@Slf4j
public class TaskCacheServiceImpl implements TaskCacheService {

    @Autowired
    private RedisCache redisCache;

    @Override
    public void add(String remindType, Integer userId, Integer orderId) {
        String key = remindType + userId + ":" + orderId;

        Integer redisVal = redisCache.getCacheObject(key);
        if (redisVal == null) {
            redisVal = 0;
        }
        redisVal = redisVal + 1;
        redisCache.setCacheObject(key, redisVal);
        log.debug(" ### 新增提醒:{}",key);
    }

    @Override
    public void reduce(String remindType, Integer userId, Integer orderId) {
        String key = remindType + userId + ":" + orderId;
        redisCache.deleteObject(key);
        log.debug(" ### 删除提醒:{}",key);
    }

    @Override
    public void delete(String remindType, Integer orderId) {
        String key = remindType + "*"+orderId;
        Collection<String> keys = redisCache.keys(key);
        for (String k : keys) {
            redisCache.deleteObject(k);
            log.debug(" ### 删除提醒:{}",k);
        }
    }

    @Override
    public Integer get(String remindType, Integer userId, Integer orderId) {
        String key = remindType + userId + ":" + orderId;
        return redisCache.getCacheObject(key);
    }

    @Override
    public Integer get(String remindType, Integer orderId) {
        String key = remindType + ":*"+ orderId;
        Collection<String> results = redisCache.keys(key);
        return results.size();
    }

    @Override
    public int count(String remindType, Integer userId) {
        String key = remindType + userId + ":*";
        Collection<String> results = redisCache.keys(key);
        return results.size();
    }


    @Override
    public Collection<String> list(String remindType, Integer userId) {
        String key = remindType + userId + ":*";
        return redisCache.keys(key);
    }


}
